package com.lvtulife.paas.gtd.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.toolkit.CollectionUtil;
import com.lvtulife.common.component.exception.ErrMsg;
import com.lvtulife.common.util.DateUtil;
import com.lvtulife.paas.gtd.bean.StrategySumVo;
import com.lvtulife.paas.gtd.entity.GtdStrategy;
import com.lvtulife.paas.gtd.service.GtdStrategyServiceI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lvtulife.paas.gtd.mapper.GtdStrategySumMapper;
import com.lvtulife.paas.gtd.entity.GtdStrategySum;
import com.lvtulife.paas.gtd.service.GtdStrategySumServiceI;
import com.lvtulife.common.service.impl.BaseServiceImpl;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.util.Date;
import java.util.List;

/**
 * GtdStrategySum 表数据服务层接口实现类
 */
@Service
@Transactional(readOnly = true)
public class GtdStrategySumServiceImpl extends BaseServiceImpl<GtdStrategySumMapper, GtdStrategySum> implements GtdStrategySumServiceI {

    @Autowired
    protected GtdStrategyServiceI strategyService;

    @Override
    @Transactional(readOnly = false, propagation = Propagation.REQUIRED)
    public Long sumStrategy(StrategySumVo vo) {
        GtdStrategySum entity = initBean(vo);

        checkOperation(baseMapper.insert(entity));
        return entity.getId();
    }

    @Override
    @Transactional(readOnly = false, propagation = Propagation.REQUIRED)
    public void delSumStrategy(Long id) {
        verifyParams(id);

        checkOperation(baseMapper.deleteById(id));
    }

    @Override
    @Transactional(readOnly = false, propagation = Propagation.REQUIRED)
    public void multiDelSumStrategy(Long[] ids) {
        verifyParams(new Object[]{ids});

        for (Long id : ids) {
            delSumStrategy(id);
        }
    }

    @Override
    @Transactional(readOnly = false, propagation = Propagation.REQUIRED)
    public void clearSumStrategy(Long strategyId, Long userId) {
        verifyParams(strategyId, userId);

        GtdStrategy entity = strategyService.selectById(strategyId);
        Assert.notNull(entity, ErrMsg.NO_OBJECT.getMsg());

        if (!entity.getUid().equals(userId)) {
            throw new RuntimeException(ErrMsg.PERMISSION_DENIED.getMsg());
        }

        List<GtdStrategySum> sumList = findSumStrategy(strategyId);
        if (CollectionUtil.isNotEmpty(sumList)) {
            for (GtdStrategySum sum : sumList)
                delSumStrategy(sum.getId());
        }
    }

    @Override
    public List<GtdStrategySum> findSumStrategy(Long strategyId) {
        return selectList(getDefaultWrapper(strategyId));
    }


    /* 初始实体类 */
    private GtdStrategySum initBean(StrategySumVo vo) {
        verifyParams(vo.getStrategyId(), vo.getContent(), vo.getSumUp());

        GtdStrategySum entity = null;
        if (vo.getId() != null) {
            entity = getEntity(vo.getId());
        } else {
            entity = new GtdStrategySum();
            entity.setSid(vo.getStrategyId());
            entity.setSumDt(new Date());
            entity.setSumYear(Integer.parseInt(DateUtil.getYear()));
        }
        entity.setSumUp(vo.getSumUp());
        entity.setSumContent(vo.getContent());

        return entity;
    }

    /* 设置查询参数 */
    private EntityWrapper<GtdStrategySum> getDefaultWrapper(Long strategyId) {
        verifyParams(strategyId);

        EntityWrapper<GtdStrategySum> wrapper = new EntityWrapper<>();
        wrapper.where("sid={0}", strategyId);
        return wrapper;
    }
}